package cn.stylefeng.guns.modular.basic.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicStockuparea;
import cn.stylefeng.guns.modular.basic.entity.BasicWarehouse;
import cn.stylefeng.guns.modular.basic.model.params.BasicStockupareaParam;
import cn.stylefeng.guns.modular.basic.service.BasicStockupareaService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 备货区表控制器
 */
@Controller
@RequestMapping("/basicStockuparea")
public class BasicStockupareaController extends BaseController {

    private String PREFIX = "/basic/basicStockuparea";

    @Autowired
    private BasicStockupareaService basicStockupareaService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicWarehouseService basicWarehouseService;

    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/basicStockuparea.html";
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	
    	//查询可选择仓库
    	model.addAttribute("wlist", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/basicStockuparea_add.html";
    }

    /**
     * 编辑页面
     */
    @RequestMapping("/edit")
    public String edit(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	
    	//查询可选择仓库
    	model.addAttribute("wlist", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/basicStockuparea_edit.html";
    }

    /**
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BasicStockupareaParam basicStockupareaParam) {
    	Object Warehouseid = basicStockupareaParam.getWarehouseid();
    	if (Warehouseid!=null) {
			BasicWarehouse bawBasicWarehouse = basicWarehouseService.getById(Integer.parseInt(Warehouseid.toString()));
			basicStockupareaParam.setWarehousename(bawBasicWarehouse.getWarehouseName());
			basicStockupareaParam.setWarehousenumber(bawBasicWarehouse.getWarehouseNumber());
		}

        //set 交付id
    	Long DeliverId = userService.getDeliverId();
    	if (DeliverId!=null) {
    		basicStockupareaParam.setDeliverId(DeliverId);
		}
        this.basicStockupareaService.add(basicStockupareaParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BasicStockupareaParam basicStockupareaParam) {
    	Object Warehouseid = basicStockupareaParam.getWarehouseid();
    	if (Warehouseid!=null) {
			BasicWarehouse bawBasicWarehouse = basicWarehouseService.getById(Integer.parseInt(Warehouseid.toString()));
			basicStockupareaParam.setWarehousename(bawBasicWarehouse.getWarehouseName());
			basicStockupareaParam.setWarehousenumber(bawBasicWarehouse.getWarehouseNumber());
		}
        this.basicStockupareaService.update(basicStockupareaParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BasicStockupareaParam basicStockupareaParam) {
        this.basicStockupareaService.delete(basicStockupareaParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BasicStockupareaParam basicStockupareaParam) {
        BasicStockuparea detail = this.basicStockupareaService.getById(basicStockupareaParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BasicStockupareaParam basicStockupareaParam,String condition ) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	basicStockupareaParam.setDeliverId(deliverId);
        return this.basicStockupareaService.findPageBySpec(basicStockupareaParam,condition,deptId,user.getOperateId());
    }

}


